package com.example.friendfindertwo.application;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import model.Category;
import model.Event;
import model.Friend;

import com.example.friendfindertwo.ParseDatabase;
import com.parse.Parse;

import android.app.Application;
import android.app.ProgressDialog;
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
public class StartApplication extends Application {

	public static final String ID_APP ="TqoeslKt3kjTT52eifO6XVoWAUvwue9uBOCE0RDa";
	public static final String ID_CLIENT = "FbAS9Uq5f52BoMbKiytqbbln9tttHUNY8ilaVpRx";
	private ParseDatabase databse = new ParseDatabase();
	
	//static attribute declaration
	private static List <Event> eventList = new ArrayList <Event>();
	private static List <Category> categoryList = new ArrayList <Category>();
	private static List<String> categoryNames = new ArrayList<String>();
	private static HashMap<String, Friend> userList = new HashMap<String, Friend>();
	private static String[] friendShip;
	private static Friend currentUser;
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		
		Parse.initialize(this, ID_APP, ID_CLIENT);
		
		new DownloadTask().execute();

		
	}
	
	public static List<Event> getAllEvents(){
		return eventList;
	}
	
	public static List<Category> getAllCategories(){
		return categoryList;
	}
	
	public static List<String> getAllCategoryNames(){
		return categoryNames;
	}

	public static void addEvent(Event e){
		eventList.add(e);
	}
	
	public static HashMap<String, Friend> getAllUsers(){
		return userList;
	}
	
	public static String[] getFriendShip(){
		return friendShip;
	}
	
	public static Friend getCurrentUser(){
		return currentUser;
	}
	

/**
 * inner async task class to download events and categories
 * @author Fiona
 *
 */
	private class DownloadTask extends AsyncTask{
		ProgressDialog myPd_ring;
		
		@Override
        protected void onPreExecute() {
//            myPd_ring  = new ProgressDialog (StartApplication.this);
//            myPd_ring.setMessage("loading..");
//            myPd_ring.show();
        }

		@Override
		protected Object doInBackground(Object... arg0) {
			eventList = databse.getEvents();
			categoryList = databse.getAllCategories();
			userList = databse.getAllUsers();
			friendShip = databse.getFriends();
			currentUser = databse.getFriendByName(ParseDatabase.getAccountName());
			for(Category cat : categoryList){
				categoryNames.add(cat.getName());
			}
			return null;
		}
		
		@Override
		protected void onPostExecute(Object result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
//			Log.i("before toString", "");
//			Log.i("eventList.size", ""+eventList.size());
//			for(Event e : eventList){
//				Log.i("start application", e.toString());
//			}
//			Log.i("after toString", "");

//			myPd_ring.dismiss();
		}
		
	}
}
